#include <iostream>
#include <mysql/mysql.h>

const char* host = "8.210.143.192";
const char* user = "zwz";
const char* passwd = "zwz04";
const char* db = "test1";
const unsigned int port = 3306;

int main()
{
    MYSQL* my = mysql_init(NULL);
    if(nullptr == my)
    {
        std::cout << "mysql init error" << std::endl;
        return 1;
    }
    mysql_real_connect(my, host, user, passwd, db, port, nullptr, 0);
    if(nullptr == my)
    {
        std::cout << "mysql connect error" << std::endl;
        return 2;
    }

    int n = mysql_query(my, "select * from t1");
    if(n != 0)
    {
        std::cout << "mysql query error" << std::endl;
    }
    MYSQL_RES* ret = mysql_store_result(my);
    int rows = mysql_num_rows(ret);
    int fields = mysql_num_fields(ret);
    MYSQL_FIELD* field = mysql_fetch_field(ret);
    for(int j = 0; j < fields; j++)
    {
        std::cout << field[j].name << "\t";
    }
    std::cout << std::endl;
    for(int i = 0; i < rows; i++)
    {
        MYSQL_ROW row = mysql_fetch_row(ret);
        for(int j = 0; j < fields; j++)
            std::cout << row[j] << "\t";
        std::cout << std::endl;
    }
    

    mysql_free_result(ret);
    mysql_close(my);
    return 0;
}